<?php
/**
 * 公司车主
 * @Author    紫梦
 * @DateTime  2018-01-26
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\Company as CompanyModel;
use app\common\model\Cars;
use app\common\request\Oss;

class Company extends Base
{
	
    public function index()
    {
        $model = CompanyModel::all();
        $this->assign('data',$model);
        return $this->fetch();
    }

    /**
     * 记录详情
     */
    public function detail($id = 1)
    {
        try {           
            $model = CompanyModel::get($id);                      
            
        } catch (\Exception $e) {
            
            return [
                'code'=>2,
                'msg'=>$e->getMessage(),
                ];
        }
        return $model;
    }

    /**
     * 添加
     */
    public function add()
    {
        $request = Request::instance();
        if($request->isAjax())
        {
            $msg['status'] = 0;
            $data = $request->post();

            //公司名是否重复
            $name  = CompanyModel::get(['name' => $data['name']]);
            if($name)
            {
                $msg['msg'] = '公司名已经存在';
                $msg['status'] = 1;

                return $msg;
            }
            unset($name);

            $file = $request->file('uploadfile');
            $model = new CompanyModel();

            // 文件上传
            if($file)
            {
                $oss = new Oss();
                $data['uploadfile'] = $oss->uploadOss($file);
            }


            $model->data($data);
            $res = $model->allowField(true)->save();

            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = $res;
            }

            return $msg;
                    
        }else{

            return $this->fetch();
        }        
              
    }

    /**
     * 编辑记录
     */
    public function edit($id)
    {
        $request = Request::instance();


        if($request->isAjax())
        {
            $msg['status'] = 0;
            $data = $request->post();
            $file = $request->file('uploadfile');
            $model = new CompanyModel();

            // 文件上传
            if($file)
            {
                $oss = new Oss();
                $data['uploadfile'] = $oss->uploadOss($file);
            }



            $res = $model->allowField(true)->isUpdate(true)->save($data);

            if($res){

                $msg['msg'] = '修改成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = $res;
            }

            return $msg;
                    
        }else{

            $model = CompanyModel::get($id);

            $this->assign('data',$model);
            return $this->fetch();
        }
    }

    /**
     * 删除记录
     */
    public function delete($id)
    {
        try {
            CompanyModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }

    public function status($id,$status)
    {
        try {
            CompanyModel::where('id', $id)
                        ->update(['status' => $status]);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'更新成功'
            ];
    }

    /**
     * 删除附件
     */
    public function del_file($id)
    {
        
        $url = Db::name('Company_file')->where(['id'=>$id])->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){
           
            try {
                Db::name('Company_file')->delete($id);
            } catch (Exception $e) {
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage(),
                    ];
            }
            
        }
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }



}